extends Node

const MOD_ID = "HollowBamboo-StartItems"
const MOD_DIR = "HollowBamboo-StartItems/"
const LOG = "HollowBamboo-StartItems"
const CONFIG_NAME = "start-items-config"

var dir = ""
var ext_dir = ""

func _init(modLoader = ModLoader):
	ModLoaderLog.info("Init", LOG)
	dir = "res://mods-unpacked/" + MOD_DIR
	ext_dir = dir + "extensions/"

	# Add extensions
	ModLoaderMod.install_script_extension(ext_dir + "singletons/menu_data.gd")
	ModLoaderMod.install_script_extension(ext_dir + "singletons/run_data.gd")

	# Add translations
	var transDir = dir + "translations/";
	ModLoaderMod.add_translation(transDir + "start_items_text.en.translation")
	ModLoaderMod.add_translation(transDir + "start_items_text.zh.translation")

	

func _ready():
	var current_config = ModLoaderConfig.get_current_config(MOD_ID)
	if current_config == null:
		ModLoaderLog.info("config [%s] is null. create new config." % MOD_ID, LOG)
		var defaultConfig = ModLoaderConfig.get_default_config(MOD_ID)
		current_config = ModLoaderConfig.create_config(MOD_ID, CONFIG_NAME, defaultConfig.data)
		ModLoaderConfig.set_current_config(current_config)
		
	if current_config.name != CONFIG_NAME:
		ModLoaderLog.info("config [%s] is default. create new config." % MOD_ID, LOG)
		current_config = ModLoaderConfig.create_config(MOD_ID, CONFIG_NAME, current_config.data)
		ModLoaderConfig.set_current_config(current_config)
	ModLoaderLog.info("Added config file: '%s'." % current_config.save_path, LOG)
	
	var ModsConfigInterface = get_node("/root/ModLoader/dami-ModOptions/ModsConfigInterface")
	if ModsConfigInterface != null:
		# connect signal
		ModsConfigInterface.connect("setting_changed", self, "_setting_changed")
		# update config to optional
		var config = ModsConfigInterface.mod_configs[MOD_ID]
		if current_config != null && current_config.name != ModLoaderConfig.DEFAULT_CONFIG_NAME:
			config.merge(current_config.data, true)
	ModLoaderLog.info("Done", LOG)
	
	
func _setting_changed(setting_name, value, mod_name) -> void:
	if mod_name == MOD_ID:
		# save setting changed
		ModLoaderLog.debug("'%s' => '%s'." % [setting_name, value], LOG)
		var config = ModLoaderConfig.get_current_config(MOD_ID)
		if config != null:
			config.data[setting_name] = value
			ModLoaderConfig.update_config(config)
		

	
	
